//
//  PopularCityView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/13.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol PopularCityViewDelegate {
    @objc optional func selectPopularCity(name:String) //选择热门城市
}

class PopularCityView: UIView {
    
    weak var popularDelegate : PopularCityViewDelegate?
    
    fileprivate let cellId = "popularCell"
    
    fileprivate var dataArray = ["北京","上海","广州","深圳","武汉","天津","西安","南京","杭州","成都","重庆"]
    
    var collectionView : UICollectionView!

    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.clear
        
        let titleLabel = UILabel()
        titleLabel.text = popularCity
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        self.addSubview(titleLabel)
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = RATIO_H(maxNum: 10)
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize.init(width: ScreenW/7*2, height: RATIO_H(maxNum: 30))
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.isScrollEnabled = false
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(TypeCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        let grayView = UIView()
        grayView.backgroundColor = UIColor.lineColor()
        self.addSubview(grayView)
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 10))
            make.left.equalToSuperview().offset(RATIO(maxNum: 8))
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        collectionView.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel)
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
            make.top.equalTo(titleLabel.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.height.equalTo(RATIO_H(maxNum: 200))
        }
        
        grayView.snp.makeConstraints { (make) in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension PopularCityView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? TypeCell
        cell?.contentView.backgroundColor = UIColor.white
        cell?.titleLabel.text = dataArray[indexPath.row]
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        self.popularDelegate?.selectPopularCity!(name: dataArray[indexPath.row])
    }
    
}
